package com.elong.businesstravel.base.baseactivity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat.Action;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.utils.CoordinateConversion;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.mapapi.map.ItemizedOverlay;
import com.baidu.mapapi.map.LocationData;
import com.baidu.mapapi.map.MKMapViewListener;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.OverlayItem;
import com.baidu.mapapi.map.PopupOverlay;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.baidusdk.location.BaiduLocation;
import com.elong.businesstravel.base.baidusdk.map.BaiduMapView;
import com.elong.businesstravel.base.baidusdk.map.TouchMapView;
import com.elong.businesstravel.base.view.TitleNavBarView;

/**
 * 百度地图基类，用来实现每个地图界面都会有的方法
 * 
 * @author lenovo
 * 
 */
abstract public class BaseMapActivity extends BaseActivity {
	
	protected static class ShowPopupOverlay {
		public String hotelName;
		public String hotelAddress;
		public GeoPoint p1;
		public int index;
	}

	protected TouchMapView mMapView = null; // 地图View
	protected MapController mMapController = null;
	protected BaiduMapView mBaiduMapView;

	// 定位相关
	protected LocationData locData = null;

	// protected boolean isRequest = false;// 是否手动触发请求定位
	// protected boolean isFirstLoc = true;// 是否首次定位
	
	/** true显示弹出框，false不显示弹出框 */
	protected boolean mShowPopupFlag = true;
	/** 显示对话框的数据 */
	protected ShowPopupOverlay mShowPopupOverlay = new ShowPopupOverlay();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	protected void initBaiduMap(MapView mapView) {
		mMapView = (TouchMapView) findViewById(R.id.mapView);
		mMapController = mMapView.getController();
		mBaiduMapView = new BaiduMapView(this, mMapView);
		// 定位初始化
		locData = new LocationData();
		// 设置定位数据
		mBaiduMapView.setLocationOverlayData(locData);
	}

	protected void baiduLocation() {
		HandlerToastUI.getHandlerToastUI(mContext, "正在定位......");
		BaiduLocation.getDefaultBaiduLocation(mApplication).setLocationListener(
				new BDLocationListener() {

					@Override
					public void onReceivePoi(BDLocation arg0) {

					}

					@Override
					public void onReceiveLocation(BDLocation location) {
						BaiduLocation.getDefaultBaiduLocation(mApplication)
								.stopLocation();
						
						if(null == mContext || ((Activity)mContext).isFinishing()){
							return ;
						}
						
						if (null == location || 0 == location.getLatitude()
								|| 0 == location.getLongitude()) {
							HandlerToastUI.getHandlerToastUI(mContext, "定位失败");
							return;
						}
						mApplication.mBdLocation = location;

						locData.latitude = location.getLatitude();
						locData.longitude = location.getLongitude();
						// 如果不显示定位精度圈，将accuracy赋值为0即可
						locData.accuracy = location.getRadius();
						// 此处可以设置 locData的方向信息, 如果定位 SDK
						// 未返回方向信息，用户可以自己实现罗盘功能添加方向信息。
						locData.direction = location.getDerect();
						// 更新定位数据
						mBaiduMapView.setLocationOverlayData(locData);
						// 更新图层数据执行刷新后生效
						mMapView.refresh();

						GeoPoint locGeoPoint = new GeoPoint(
								(int) (locData.latitude * 1e6),
								(int) (locData.longitude * 1e6));

						// 是手动触发请求或首次定位时，移动到定位点
						// if (isRequest || isFirstLoc) {
						// 移动地图到定位点
						Log.d("LocationOverlay",
								"receive location, animate to it");
						mMapController.animateTo(locGeoPoint);
						// isRequest = false;
						// mBaiduMapView.setLocationMode(LocationMode.FOLLOWING);

						HandlerToastUI.getHandlerToastUI(mContext, "定位成功");
						// }
						// 首次定位完成
						// isFirstLoc = false;

					}
				});
		BaiduLocation.getDefaultBaiduLocation(mContext).startLocation();
	}

	@Override
	protected void onPause() {
		if (null != mMapView) {
			mMapView.onPause();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		if (null != mMapView) {
			mMapView.onResume();
		}
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		if (null != mMapView) {
			mMapView.destroy();
		}
		super.onDestroy();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		if (null != mMapView) {
			mMapView.onSaveInstanceState(outState);
		}

	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		if (null != mMapView) {
			mMapView.onRestoreInstanceState(savedInstanceState);
		}
	}

}
